package test.project;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CloudSolrClient;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrInputDocument;

import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.druid.DruidPlugin;

public class DataImport {
	
	static String url = "jdbc:mysql://10.10.3.30/licai?characterEncoding=UTF-8";
	static String username = "kangrui";
	static String password = "kangrui123456";
	static int pageSize = 1000;
	
	public static void main(String[] args) {
		DruidPlugin dp = new DruidPlugin(url, username, password);
		ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
		arp.setDevMode(true);
		arp.setShowSql(true);
		dp.start();
		arp.start();
		
		CloudSolrClient client = new CloudSolrClient("192.168.5.34:2181");
		
		List<Record> list = Db.find("select * from lc_article");
		List<SolrInputDocument> docs = new ArrayList<>();
		list.forEach(new Consumer<Record>() {
			@Override
			public void accept(Record t) {
				Integer id = t.getInt("id");
				Integer tag_id = t.getInt("tag_id");
				Integer is_stick = t.getInt("is_stick");
				Integer is_spread = t.getInt("is_spread");
				Integer channel_id = t.getInt("channel_id");
				
				String image = t.getStr("image");
				
				String title = t.getStr("title");
				String keywords = t.getStr("keywords");
				String intro = t.getStr("intro");
				String text = t.getStr("text");
				
				Date ctime = t.getDate("ctime");
				
				SolrInputDocument doc = new SolrInputDocument();
				doc.addField("id", id);
				doc.addField("tag_id", tag_id);
				doc.addField("is_stick", is_stick);
				doc.addField("is_spread", is_spread);
				doc.addField("channel_id", channel_id);
				doc.addField("image", image);
				doc.addField("title", title);
				doc.addField("keywords", keywords);
				doc.addField("intro", intro);
				doc.addField("ctime", ctime);
				doc.addField("text", delHTMLTag(text));
				
				docs.add(doc);
			}
		});
		
		try {
			client.add("article", docs);
			client.commit("article");
		} catch (SolrServerException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				client.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	private static String delHTMLTag(String htmlStr){ 
		if(htmlStr==null) return null;
        String regEx_script="<script[^>]*?>[\\s\\S]*?<\\/script>"; //定义script的正则表达式 
        String regEx_style="<style[^>]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式 
        String regEx_html="<[^>]+>"; //定义HTML标签的正则表达式 
         
        Pattern p_script=Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE); 
        Matcher m_script=p_script.matcher(htmlStr); 
        htmlStr=m_script.replaceAll(""); //过滤script标签 
         
        Pattern p_style=Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE); 
        Matcher m_style=p_style.matcher(htmlStr); 
        htmlStr=m_style.replaceAll(""); //过滤style标签 
         
        Pattern p_html=Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE); 
        Matcher m_html=p_html.matcher(htmlStr); 
        htmlStr=m_html.replaceAll(""); //过滤html标签 

        return htmlStr.trim(); //返回文本字符串 
    } 

	@org.junit.Test
	public void testDel() {
		CloudSolrClient client = new CloudSolrClient("192.168.5.34:2181");
		try {
			UpdateResponse ur = client.deleteByQuery("article", "*:*");
			client.commit("article");
			System.out.println(ur);
		} catch (SolrServerException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				client.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
}
